package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Result;
import com.example.domain.Student;
import com.example.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/student")
public class studentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/page")
    public Result<Page> page (int size,int pageSize,String name) {
        Page<Student> studentPage = new Page<>(size, pageSize);
        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentLambdaQueryWrapper.like(!StringUtils.isEmpty(name),Student::getName,name);
        studentLambdaQueryWrapper.orderByAsc(Student::getName);
        Page<Student> studentPage1 = studentService.page(studentPage, studentLambdaQueryWrapper);
        return Result.success(studentPage1);
    }

    @PostMapping
    public Result<String> add (Student student) {
        studentService.add(student);
        return Result.success("添加成功！！！");
    }

    @GetMapping("/{id}")
    public Result<Student> updateById (@PathVariable Integer id) {
        Student student = studentService.updateStudentById(id);
        return Result.success(student);
    }


    @PutMapping
    public Result<String> update (@RequestBody Student student) {
        studentService.updateStudent(student);
        return Result.success("修改成功！！！");
    }

    @DeleteMapping("/{id}")
    public Result<String> delete (@PathVariable Integer id) {
        studentService.deleteStudent(id);
        return Result.success("删除成功！！！");
    }
}
